package com.luanmad.ajax.dtree.action;

import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.googlecode.jsonplugin.annotations.JSON;
import com.luanmad.ajax.dtree.manager.TreeNodesManager;
import com.luanmad.ajax.dtree.model.Style;
import com.luanmad.ajax.dtree.model.TreeNodes;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 类说明
 * 
 * @author luanmad!
 * @version V1.0 创建时间：2009-8-17 下午10:38:18 
 * share forum:www.zoomhoo.com
 */
public class TreeListAction extends ActionSupport
{
	private TreeNodes treeNodes;
	private TreeNodesManager treeNodesManager;
	private List<TreeNodes> treeNodesList;
	private String treeNodesString;
	private Style style;
	private Map<String, Integer> parentNodesMap = new HashMap<String, Integer>();
	private List<TreeNodes> parentNodesList = new ArrayList<TreeNodes>();
	private String move;

	public String getMove()
	{
		return move;
	}

	public void setMove(String move)
	{
		this.move = move;
	}

	public List<TreeNodes> getParentNodesList()
	{
		return parentNodesList;
	}

	public void setParentNodesList(List<TreeNodes> parentNodesList)
	{
		this.parentNodesList = parentNodesList;
	}

	public Map<String, Integer> getParentNodesMap()
	{
		return parentNodesMap;
	}

	public void setParentNodesMap(Map<String, Integer> parentNodesMap)
	{
		this.parentNodesMap = parentNodesMap;
	}

	public Style getStyle()
	{
		return style;
	}

	public void setStyle(Style style)
	{
		this.style = style;
	}

	public String getTreeNodesString()
	{
		return treeNodesString;
	}

	public void setTreeNodesString(String treeNodesString)
	{
		this.treeNodesString = treeNodesString;
	}

	@JSON(serialize = false)
	public TreeNodesManager getTreeNodesManager()
	{
		return treeNodesManager;
	}

	public void setTreeNodesManager(TreeNodesManager treeNodesManager)
	{
		this.treeNodesManager = treeNodesManager;
	}

	@JSON(name = "treeList")
	public List<TreeNodes> getTreeNodesList()
	{
		return treeNodesList;
	}

	public void setTreeNodesList(List<TreeNodes> treeNodesList)
	{
		this.treeNodesList = treeNodesList;
	}

	@JSON(serialize = false)
	public TreeNodes getTreeNodes()
	{
		return treeNodes;
	}

	public void setTreeNodes(TreeNodes treeNodes)
	{
		this.treeNodes = treeNodes;
	}

	/**
	 * 加载树
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception
	{

		treeNodesString = createTreeNodes();
		return SUCCESS;
	}

	public String add() throws Exception
	{
		
		// treeNodes.setPid(treeNodes.getPid());
		// treeNodes.setName(treeNodes.getName());
		treeNodes.setName(URLDecoder.decode(URLDecoder.decode(treeNodes.getName(), "utf-8"), "utf-8"));
		// treeNodes.setUrl("/index.jsp");
		treeNodes.setTitle(treeNodes.getName());
		// treeNodes.setSort(sort);
		treeNodes.setIcon("images/ajax_dtree/folder.gif");
		treeNodes.setFontStyleName(style.getFontStyle() + style.getFontSize()
									+ ";color:" + style.getFontColor());

		treeNodesManager.addNode(treeNodes);

		return SUCCESS;
	}

	public String update() throws Exception
	{

		//为了兼容火狐与IE的编码问题，在客户端需进行两次编码（使用UTF-8编码），在此虽两次解码后再将值插入到数据库中。
		treeNodes.setName(URLDecoder.decode(URLDecoder.decode(treeNodes.getName(), "utf-8"), "utf-8"));
		treeNodes.setTitle(treeNodes.getName());
		treeNodes.setIcon("images/ajax_dtree/folder.gif"); 
		treeNodes.setFontStyleName(style.getFontStyle() + style.getFontSize()
									+ ";color:" + style.getFontColor());

		treeNodesManager.updateNode(treeNodes);
		return SUCCESS;
	}

	/**
	 * 列出所有节点作为更新节点时的父节点
	 * @return
	 * @throws Exception
	 */
	public String listParentNodes() throws Exception
	{
		try
		{
			parentNodesList = treeNodesManager.queryNodes("From TreeNodes as t Order By sort");

		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}

		return SUCCESS;
	}

	
	public String delete() throws Exception
	{

		treeNodesManager.deleteNode(treeNodes.getId());
		return SUCCESS;
	}

	/**
	 * 节点排序
	 * @return
	 * @throws Exception
	 */
	public String sort() throws Exception
	{

		treeNodesManager.sortNode(treeNodes, move);
		return SUCCESS;
	}

	/**
	 * 构建树。
	 * @return
	 */
	public String createTreeNodes()
	{
		StringBuffer treeListBuffer = new StringBuffer();

		try
		{
			treeNodesList = treeNodesManager.queryNodes("From TreeNodes as t Order By sort");
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}

		TreeNodes treeNodesContent = null;

		for (int i = 0; i < treeNodesList.size(); i++)
		{
			System.out.println("i=" + i);
			treeNodesContent = treeNodesList.get(i);

			// 构树时的结点次序要严格固定好，不能篡序。
			// Node(id, pid, name, url, title, target, icon, sort,
			// fontStyleName, iconOpen, open)
			// 如d.add(0, -1, 'luanamed', 'luanmad2009.s156.eatj.com', 'Hi
			// Dtree', '_blank', '/images/ajax_dtree/folder.gif', 0, '','','');
			treeListBuffer.append("d.add(").append(treeNodesContent.getId()).append(",") // id
			.append(treeNodesContent.getPid()).append(",") // pid
			.append("\"").append(treeNodesContent.getName()).append("\",") // name
			.append("\"").append(treeNodesContent.getUrl()).append("\",") // url
			.append("\"").append(treeNodesContent.getTitle()).append("\",") // title
			.append("\"").append(treeNodesContent.getTarget()).append("\",") // target
			.append("\"").append(treeNodesContent.getIcon()).append("\",") // icon
			// E:\Works\trunk\ctoshop\project\shopAdmin\resources\StoreAdmin\images\ajax_dtree\img\folder.gif
			.append(treeNodesContent.getSort()).append(",") // sort
			// .append("\"").append("<font style=").append(treeNodesContent.getFontStyleName()).append(">").append(treeNodesContent.getName()).append("</font>").append("<img src='").append("images/ajax_dtree/available.gif'").append(" alt='类别显示' />").append("\"")
			// // fontStyle+name+img(is_available)
			.append("\"").append("<font style='").append(treeNodesContent.getFontStyleName()).append("'>").append(treeNodesContent.getName()).append("</font>").append("\"") // fontStyle+name+img(is_available)
			.append(");\n");
		}

		treeListBuffer.append("d.draw();");
		System.out.println("11treeList=" + treeListBuffer.toString());
		return treeListBuffer.toString();

	}
}
